<div class="layui-breadcrumb nepadmin-breadcrumb">
    <a>
        <cite>编辑权限</cite>
    </a>
</div>
<script type="text/html" template>
    <div class="layui-fluid" id="VIEW-list-table" lay-title="表格列表">
        <div class="layui-card">
            <input type="hidden" value="{{d.roleId}}" id="roleId">
            <input type="hidden" value="{{d.roleName}}" id="name">
            <div class="layui-btn-container nepadmin-pad-t10 nepadmin-pad-l10">
                <div class="layui-btn layui-btn-sm" id="submit">提交</div>
                <div class="layui-btn layui-btn-primary layui-btn-sm" id="close">返回</div>
            </div>
            <div><table class="layui-hidden" id="menuListTable" lay-filter="menuListTable"></table> </div>
        </div>
    </div>
</script>
<script>
    var Authorization = function () {
        var admin = layui.admin, table = layui.table, form = layui.form, $ =layui.jquery,treeGrid = layui.treeGrid;
        //初始化列表
        this.init = function () {
            //获取权限
            admin.get({
                api: 'roleInfo',
                urlStr: "/" + $("#roleId").val(),
                success: function (resp) {
                    var checkboxMap = {};
                    if(resp.data.menuIdList){
                        resp.data.menuIdList.forEach(function (obj) {
                            checkboxMap[obj] = true;
                        });
                    }
                    //初始化权限
                    treeGrid.render({
                        id:'menuListTable'
                        ,elem: '#menuListTable'
                        ,api:'getMenuList'
                        ,idField:'id'
                        ,treeId:'id'//树形id字段名称
                        ,treeUpId:'parentId'//树形父id字段名称
                        ,treeShowName:'name'//以树形式显示的字段
                        ,checkboxMap:checkboxMap
                        ,cols: [[
                            {type:'checkbox',sort:true}
                            ,{field:'name', title: '名称' ,templet: function(d){
                                if(d.type == 0){
                                    return '<span style="color: red">'+d.name+'</span>'
                                }else if(d.type == 1){
                                    return '<span style="color: green">'+d.name+'</span>'
                                }else if(d.type == 2){
                                    return '<span style="color: #8b8b8b">'+d.name+'</span>'
                                }
                            }}
                            ,{field:'parentName',align:'center', title: '父级菜单',templet: function(d){
                                if(d.parentName){
                                    return d.parentName
                                }else {
                                    return ''
                                }
                            }}
                            ,{field:'type',align:'center', title: '类型',templet: function(d){
                                if(d.type == 0){
                                    return '<span class="layui-badge layui-bg-red">目录</span>'
                                }else if(d.type == 1){
                                    return '<span class="layui-badge layui-bg-green">菜单</span>'
                                }else if(d.type == 2){
                                    return '<span class="layui-badge layui-bg-gray">按钮</span>'
                                }
                            }}
                            ,{field:'orderNum',align:'center', title: '排序'}
                        ]]
                    });
                }
            });
            //关闭窗口
            $("#close").click(function () {
                admin.refresh();
            });
            //提交
            $("#submit").click(function () {
                var checkStatus = treeGrid.checkStatus('menuListTable');
                var requestData = {};
                requestData.roleId = $("#roleId").val();
                requestData.roleName = $("#name").val();
                var menuIdList = [];
                checkStatus.data.forEach(function (obj) {
                    menuIdList.push(obj.id);
                });
                requestData.menuIdList = menuIdList;
                admin.post({
                    api:'updateRole',
                    data:JSON.stringify(requestData),
                    contentType: 'application/json;charset=UTF-8',
                    success:function (res) {
                        if(res.code == 0){
                            admin.success('操作成功',function () {
                                admin.refresh();
                            });
                        }
                    }
                });
            });
        };
    };
</script>